package utils
{
	import caurina.transitions.Tweener;
	
	import flash.events.Event;
	import flash.text.TextLineMetrics;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Label;
	import mx.core.Application;
	import mx.core.UIComponent;
	import mx.managers.PopUpManager;
	
	import ui.notifications.MessageNotification;
	
	public class MessageBus extends Object
	{
		private static var notifications:ArrayCollection = new ArrayCollection();
		
		private static var verticalGap:Number = 10;
		
		private static var paddingRight:Number = 15;
		private static var paddingTop:Number = 15;
		
		public function MessageBus()
		{
			super();
		}
		
		public static function post(message:String):void{			
			var yPosition:Number = paddingTop;
			for each(var child:MessageNotification in notifications){
				yPosition += child.height;
				yPosition += verticalGap;
			}
			
			var msg:MessageNotification = new MessageNotification();
			msg.text = message;
			msg.addEventListener("expired", onMessageExpired);
			msg.popUp();
			msg.autoResize();
			msg.x = Application.application.width - msg.width - paddingRight;
			msg.y = yPosition;
			
			notifications.addItem(msg);
		}
		
		private static function onMessageExpired(evt:Event):void{
			notifications.removeItemAt(0);
			
			// Move all children up
			for each(var child:MessageNotification in notifications){
				var yPosition:Number = child.y - (evt.target as MessageNotification).height - verticalGap;
				Tweener.addTween(child, {y: yPosition, time: 2});
			}
			
		}
		
	}
}